package com.lwgk.basic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lwgk.AppComplication;
import com.lwgk.R;
import com.lwgk.compinterface.OnMultiClickListener;
import com.lwgk.utils.NetConnectUtils;
import com.lwgk.utils.PhoneCheckUtils;
import com.lwgk.utils.StatusBarUtils;
import com.lwgk.utils.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * created by 赵航
 * 联系微信 微信号：zhao--hang--
 * on 2019/7/12
 * --------------------------------------------------------------------------
 * 描述：所有界面公共的基础类，主要是界面的Ui层
 * 注意事项：掌握，对外的权限上到最低，不建议用public 尽量用protected，所有的变量必须用private，基础类不对外传递任何变量
 */
public abstract class BasicActivity extends AllBasicActivity {

    private LinearLayout              mActionBarView;


    private LinearLayout              mHandTitleLine;
    private RelativeLayout            mBasicLift;
    private RelativeLayout            mBasicRight;
    private TextView                  mBasicTitle;
    private LinearLayout.LayoutParams mLayoutParams;
    private LayoutInflater            mInflater;
    private FrameLayout               mContentLayout;
    //定义一个当前状态（默认正在加载中）
    private STATE                     mCurrentState = STATE.LOADING;
    private View                      mWaitingView;
    private View                      mNoNetView;
    private View                      mEmptyDataView;
    private View                      mNodataLayout;
    private View                      mNormalView;
    private SmartRefreshLayout        mSmartRefreshLayout;
    private Button                    mNoNetButton;
    private Button                    mNoDataButton;
    private ImageView                 mNoDataImage;
    private TextView                  mNoDataText;
    //定义三个状态
    private enum STATE {
        LOADING,//正在加载中
        SUCCESS,//加载成功
        EMPTYDATE,//加载失败
        NONET,;//无网界面

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置沉寝式状态栏
        StatusBarUtils.setStatusTransparent(BasicActivity.this);
        //设置文字颜色是黑色
        StatusBarUtils.setLightMode(BasicActivity.this);
        setContentView(R.layout.activity_basic);

        mActionBarView = (LinearLayout) findViewById(R.id.action_bar_view);

        //首先判断是不是pad，
        if (!PhoneCheckUtils.isPad()) {
            if (AppComplication.mActionBarSize == 0) {
                AppComplication.mActionBarSize = StatusBarUtils.getActionBarHeight();
            }
            //设置状态栏的宽度
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, AppComplication.mActionBarSize);
            mActionBarView.setLayoutParams(params);
        }

        //标题栏，如果需要自定义的，可以隐藏
        mHandTitleLine = (LinearLayout) findViewById(R.id.basic_hand_line);


        //标题栏的三个按钮，可以进行其他的操作
        mBasicLift = (RelativeLayout) findViewById(R.id.basic_left);
        mBasicRight = (RelativeLayout) findViewById(R.id.basic_right);
        mBasicTitle = (TextView) findViewById(R.id.basic_title);


        //重新加载按钮
        mNoNetButton = (Button) findViewById(R.id.basic_no_net_button);
        mNoDataButton = (Button) findViewById(R.id.basic_no_data_button);
        mNoDataImage = (ImageView) findViewById(R.id.basic_no_data_image);
        mNoDataText = (TextView) findViewById(R.id.basic_no_data_text);




        mContentLayout = (FrameLayout) findViewById(R.id.basic_content_layout);

        mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        //引入布局
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mNormalView = mInflater.inflate(setContentSuccessView(), null);
        //初始化成功的界面
        mContentLayout.addView(mNormalView, mLayoutParams);

        //初始化等待中的界面
        mWaitingView = findViewById(R.id.basic_waiting);

        //无网界面
        mNoNetView = findViewById(R.id.basic_no_net);

        //无数据界面
        mEmptyDataView = findViewById(R.id.basic_empty_data);

        //包含下拉刷新的界面
        mNodataLayout = findViewById(R.id.basic_no_data);


        //加载界面时，先初始化view
        mCurrentState = STATE.LOADING;

        //页面切换的方法
        changeView();

        //初始化view
        initView();

        //初始化数据
        initData();


        //刷新数据
        mSmartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(0);
                mCurrentState = STATE.LOADING;
                changeView();
                initData();
            }
        });
        mNoNetButton.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                changeView();
                initData();
            }
        });

        mNoDataButton.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                changeView();
                initData();
            }
        });

    }


    /**
     * @return 初始化界面
     */
    protected abstract int setContentSuccessView();

    /**
     * 初始化view
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 切换Ui
     */
    private void changeView() {
        mNodataLayout.setVisibility(View.GONE);
        mWaitingView.setVisibility(View.GONE);
        mContentLayout.setVisibility(View.GONE);
        //根据当前状态切换
        switch (mCurrentState) {
            case LOADING:
                mWaitingView.setVisibility(View.VISIBLE);
                break;
            case SUCCESS:
                mContentLayout.setVisibility(View.VISIBLE);
                break;
            case EMPTYDATE:
                mNodataLayout.setVisibility(View.VISIBLE);
                mEmptyDataView.setVisibility(View.VISIBLE);
                mNoNetView.setVisibility(View.GONE);
                break;
            case NONET:
                mNodataLayout.setVisibility(View.VISIBLE);
                mEmptyDataView.setVisibility(View.GONE);
                mNoNetView.setVisibility(View.VISIBLE);
                break;
        }

    }


    /**
     * 自动切换Ui，网络请求完，需要调用该方法来切换Ui,再initData中使用，网络请求完成数据后，
     *
     * @param haveNet 当前是否有网无网传 false ，有网的时候传true
     * @param obj     ，请求失败后传null ，如果有数据就传一个部位空的对象
     */
    protected void autoChengeUi(boolean haveNet, final Object obj) {
        //如果是无网状态
        if (!haveNet) {
            mCurrentState = STATE.NONET;
            changeView();
            ToastUtils.showIntTost(R.string.net_error);
            return;
        } else {
            //如果是有网状态,子线程获取数据
            //获取网络数据
            mCurrentState = checkData(obj);
            //主线程切换Ui
            changeView();
        }
    }

    /**
     * @param obj 传递过来的数据，根据数据进行自动切换Ui
     * @return
     */
    private STATE checkData(Object obj) {
        if (!NetConnectUtils.isNetConnected()) {
            return STATE.NONET;
        } else {
            if (obj == null) {//如果数据为空，则失败
                return STATE.EMPTYDATE;
            } else {
                if (obj instanceof List) {//如果返回的是数组
                    List list = (List) obj;
                    if (list.size() > 0) {
                        return STATE.SUCCESS;
                    } else {
                        return STATE.EMPTYDATE;
                    }
                } else {//如果是对象
                    return STATE.SUCCESS;
                }
            }
        }
    }

    /**
     * @return 入去当前界面的标题栏，不需要的时候可以隐藏
     */
    protected LinearLayout getHandTitleLine() {
        return mHandTitleLine;
    }

    /**
     * 设置左侧腰添加的东西，文字，状态，问题结合等，不设置是空，什么都不显示
     *
     * @param tag   为0时，设置为纯文字，1时设置为纯图片，3时，为随意自定义写的样例,如果有需要可以增加4,5,6等
     * @param resID 文件资源的id
     */
    protected void setLeftShow(int tag, int resID) {
        if (null == mBasicLift) {
            mBasicLift = (RelativeLayout) findViewById(R.id.left);
        }
        mBasicLift.removeAllViews();
        mBasicLift.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.basic_left_textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mBasicLift.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.basic_left_imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mBasicLift.addView(view, params);
        } else if (tag == 3) {
            View view = getLayoutInflater().inflate(R.layout.basic_weight_back, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mBasicLift.addView(view, params);
        }


    }

    /**
     * 设置右侧腰添加的东西，文字，状态等，默认是不添加任何东西，不设置是空，什么都不显示
     *
     * @param tag   为0时，设置为纯文字，1时设置为纯图片，3时，为随意自定义写的样例,如果有需要可以增加4,5,6等
     * @param resID 文件资源的id
     */
    protected void setRightShow(int tag, int resID) {

        if (null == mBasicRight) {
            mBasicRight = (RelativeLayout) findViewById(R.id.left);
        }
        mBasicRight.removeAllViews();
        mBasicRight.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.basic_right_textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mBasicRight.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.basic_right_imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mBasicRight.addView(view, params);
        }

    }

    /**
     * @param title 设置标题
     */
    protected void setTitleText(String title,int color) {
        if (mBasicTitle == null) {
            mBasicTitle = (TextView) findViewById(R.id.basic_title);
        }
        mBasicTitle.setTextColor(getResources().getColor(color));
        mBasicTitle.setText(title);
    }

    /**
     * @return 向外暴露方法，提供可变数据界面的按钮文字
     */
    protected Button getNoDataButton() {
        return mNoDataButton;
    }
    /**
     * @return 向外暴露方法，提供可变无数据界面的背景图片
     */
    protected ImageView getNoDataImage() {
        return mNoDataImage;
    }
    /**
     * @return 向外暴露方法，提供可变无数据界面的文字描述
     */
    protected TextView getNoDataText() {
        return mNoDataText;
    }

    /**向外暴露actionBar用于修改颜色等
     * @return
     */
    public LinearLayout getActionBarView() {
        return mActionBarView;
    }
    /**
     * 仅仅是界面的跳转，不传递任何数据
     * @param isFinsh 是否结束当前的界面，
     * @param clasz   要跳转到的界面
     */
    protected void skipOtherActivity(boolean isFinsh, Class clasz) {
        startActivity(new Intent(BasicActivity.this, clasz));
        if (isFinsh) {
            BasicActivity.this.finish();
        }
    }


}
